<?php
namespace app\model;

use think\Model;
use app\model\Bbs;
use app\model\User;
use app\model\BbsTypeFans;
use app\model\BbsComment;

class BbsType extends Model
{
    protected $table = "ea_bbs_type";

    /**
     * [getIpList 获取IP列表]
     * @since  2022-03-31
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getBbsTypeList($data=[])
    {
        $list = self::whereNull('delete_time')->where('type_status', 1)
            ->field('id, user_id, type_desc, type_name, type_pic')
            ->withCount(['bbs', 'typeFans'])
            ->order('bbs_count DESC')
            ->select();

        return $list;
    }

    /**
     * [bbs 关联动态]
     * @since  2022-04-29
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbs()
    {
        return $this->hasMany(Bbs::class, 'type_id', 'id');
    }

    /**
     * [author 圈子的作者|getCircleInfo方法中使用]
     * @since  2022-05-05
     * @author Dai哥
     * @return [type]     [description]
     */
    public function author()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field('id, nickname, avatar');
    }

    /**
     * [author 关注圈子的用户列表|getCircleInfo方法中使用]
     * @since  2022-05-05
     * @author Dai哥
     * @return [type]     [description]
     */
    public function typeFans()
    {
        return $this->hasMany(BbsTypeFans::class, 'bbs_type_id', 'id');
    }

    /**
     * [topc 远程一对多|分类对应bbs动态，动态对应]
     * @since  2022-05-05
     * @author Dai哥
     * @return [type]     [description]
     */
    public function topc()
    {
        /**
            每个城市有多个用户
            每个用户有多个话题
            城市和话题之间并无关联

            每个分类有多个动态
            每个动态有多个评价
            分类和评价之间并无关联

        */

        return $this->hasManyThrough(Bbs::class, BbsComment::class, 'id', 'bbs_id', 'id', 'id');
    }

    /**
     * [getCircleInfo 根据圈子ID查询圈子详细]
     * @since  2022-05-05
     * @author Dai哥
     * @param  string     $value [description]
     * @return [type]            [description]
     */
    public static function getCircleInfo($data)
    {
        $model = self::whereNull('delete_time')
            ->field('id, user_id, type_name, type_desc, type_pic, create_time as create_date, type_tags, type_class, type_ids')
            ->where('type_status', 1);

        if(isset($data['id']) && $data['id']){
            $model = $model->where('id', $data['id']);
        }

        $info = $model
            ->withSum('bbs', 'browse')
            ->withCount([
                # 加入圈子的总人数
                'typeFans', 

                # 当前分类动态总数
                'bbs',

                # 判断当前用户是否加入圈子
                'typeFans' =>function($query, &$alias) use($data){
                    $query->where('user_id', $data['user_id']);
                    $alias = 'check_login_user_fans';
                }
            ])
            ->with([
                'author',
                'typeFans' => function($query) use ($data){
                    $query->alias('a')->field('a.id, a.bbs_type_id, a.is_admin, a.user_id, b.nickname, b.avatar')
                        ->join('ea_user b', 'a.user_id = b.id', 'LEFT')
                        ->order('a.is_admin DESC, a.id DESC');
            },
        ])->find();

        // dump($info->toArray());exit;
        return $info;
    }

    public function getBbsSumAttr($value)
    {
        if($value){
            return $value;
        }

        return 0;
    }

    public function getCreateDateAttr($value)
    {
        return date('Y-m-d', $value);
    }

    public function getTypeTagsAttr($value)
    {
        if($value){
            return explode(',', $value);
        }
        return [];
    }

    public function getTypeClassAttr($value)
    {
        switch ($value) {
            case 1:
                $str = '公开圈子';
                break;
            
            case 2:
                $str = '私密圈子';
                break;
            
            default:
                $str = '公开圈子';
                break;
        }

        return $str;
    }
}